//
//  GetMoneyViewController.m
//  DaiShengInvest
//
//  Created by 张睿 on 2017/6/29.
//  Copyright © 2017年 davinci. All rights reserved.
//

#import "GetMoneyViewController.h"

@interface GetMoneyViewController ()<UIPickerViewDelegate,UIPickerViewDataSource>{
    NSInteger _bankIndex;   // 银行选择 记录
}

@property (nonatomic, weak)UIImageView * backImageView;

@property (nonatomic, weak)UILabel * chooseBankCardLabel;

@property (nonatomic, weak)UIButton * chooseBankButton;

@property (nonatomic, weak)UILabel * bankLabel;

@property (nonatomic, weak)UIImageView * rightImageView;

@property (nonatomic, weak)UIView * oneLineView;

@property (nonatomic, weak)UILabel * moneyCountLabel;

@property (nonatomic, weak)UILabel * moneyNumberField;

@property (nonatomic, weak)UILabel * getMoneyCountLabel;


@property (nonatomic, weak)UITextField * getMoneyCountField;

@property (nonatomic, weak)UIView * twoLineView;

@property (nonatomic, weak)UIButton * nextButton;

@property (nonatomic, strong)NSDictionary * dict;
@property (nonatomic, copy)NSString * bankNo;
@property (nonatomic, copy)NSString * bankName;
@property (nonatomic, copy)NSString * bankid;
@property (nonatomic, strong)NSArray * dataArray;

@property (nonatomic, strong)UIView * bgView;
@property (nonatomic, strong)UIPickerView * pickerView;



@end

@implementation GetMoneyViewController

- (void)viewWillAppear:(BOOL)animated{
    
    [self loadData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    _bankIndex = 0;
    [self setupNavBar];
    [self setupUI];
    [self setupPivkerView];
}


- (void)loadData{
    
    [[NetworkTool sharedTools] POST:@"user/bank-list" parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary *  _Nullable responseObject) {

        self.dataArray = responseObject[@"param"];
//        NSDictionary * dict = self.dataArray[0];
//        
//        self.bankNo = dict[@"bankNo"];
//        self.bankName = dict[@"bankName"];
//        
//        NSString * bankNum = [self.bankNo substringFromIndex:15];
//        NSString * bankInfo = [NSString stringWithFormat:@"%@(尾号%@)",self.bankName,bankNum];
//        
//        [self.bankButton setTitle:bankInfo forState:UIControlStateNormal];
        [self.pickerView reloadAllComponents];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        NSLog(@"%@",error);
    }];
    
}

- (void)setupNavBar{
    
    self.title = @"提现";
    
    UIButton * backButton = [[UIButton alloc] init];
    
    backButton.frame = CGRectMake(15 * SCREENH6, 5 * SCREENH6, 12 * SCREENH6, 20 * SCREENH6);
    [backButton setImage:[UIImage imageNamed:@"1返回"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(backToAccountView) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem * leftButton = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    
    self.navigationItem.leftBarButtonItem = leftButton;
    
}


- (void)setupUI{
    
    UIImageView * backImage = [[UIImageView alloc] init];
    
    backImage.image = [UIImage imageNamed:@"背景"];
    
    self.backImageView = backImage;
    
    [self.view addSubview:self.backImageView];
 
    UILabel * chooseBankCardLabel = [[UILabel alloc] init];
    
    chooseBankCardLabel.font = [UIFont fontWithName:@"PingFangSC-Medium" size:15 * SCREENH6];
    chooseBankCardLabel.textColor = ColorWithRGB(0xffffff);
    chooseBankCardLabel.text = @"选择银行卡";
    
    self.chooseBankCardLabel = chooseBankCardLabel;
    
    [self.view addSubview:self.chooseBankCardLabel];

    
    UIImageView * rightImageView = [UIImageView new];
    
    rightImageView.image = [UIImage imageNamed:@"修改头像"];
    
    self.rightImageView = rightImageView;
    
    [self.view addSubview:self.rightImageView];
    
    
    UIButton * chooseBankButton = [UIButton new];
    //15
//    NSString * bankNum = [self.bankNo substringFromIndex:15];
//    NSString * bankInfo = [NSString stringWithFormat:@"%@(尾号%@)",self.bankName,bankNum];
//    [bankButton setTitle:bankInfo forState:UIControlStateNormal];
    chooseBankButton.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:15 * SCREENH6];
    [chooseBankButton setTitleColor:ColorWithRGB(0xd5d5d5) forState:UIControlStateNormal];
    [chooseBankButton addTarget:self action:@selector(didChooseOtherBank) forControlEvents:UIControlEventTouchUpInside];
    [chooseBankButton setBackgroundColor:[UIColor clearColor]];
    
    self.chooseBankButton = chooseBankButton;
    
    [self.view addSubview:self.chooseBankButton];
    
    
    UILabel * bankLabel = [UILabel new];
    
    bankLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:17 * SCREENH6];
    bankLabel.textColor = ColorWithRGB(0xd5d5d5);
    
    self.bankLabel = bankLabel;
    
    [self.view addSubview:self.bankLabel];
    
    
    UIView * oneLineView = [UIView new];
    
    oneLineView.backgroundColor = [UIColor whiteColor];
    oneLineView.alpha = 0.1;
    
    self.oneLineView = oneLineView;
    
    [self.view addSubview:self.oneLineView];
    
    
    UILabel * moneyCountLabel = [[UILabel alloc] init];
    
    moneyCountLabel.font = [UIFont fontWithName:@"PingFangSC-Medium" size:15 * SCREENH6];
    moneyCountLabel.textColor = ColorWithRGB(0xc8c8c8);
    moneyCountLabel.text = @"当前可提现金额";
    
    self.moneyCountLabel = moneyCountLabel;
    
    [self.view addSubview:self.moneyCountLabel];
    
    
    UILabel * moneyNumberField = [[UILabel alloc] init];
    
    moneyNumberField.font = [UIFont fontWithName:@"PingFangSC-Medium" size:15 * SCREENH6];
    moneyNumberField.textColor = ColorWithRGB(0xc8c8c8);
    moneyNumberField.text = self.useMoney;
    
    self.moneyNumberField = moneyNumberField;
    
    [self.view addSubview:self.moneyNumberField];
    
    
    UITextField * getMoneyCountField = [[UITextField alloc] init];
    
    getMoneyCountField.borderStyle = UITextBorderStyleLine;
    getMoneyCountField.backgroundColor = ColorWithRGB(0x0a1a21);
    getMoneyCountField.layer.borderColor = [ColorWithRGB(0x102b38) CGColor];
    getMoneyCountField.layer.borderWidth = 1 * SCREENH6;
    
    //设置光标颜色
    getMoneyCountField.tintColor = ColorWithRGB(0xc8c8c8);
    NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
    attrs[NSForegroundColorAttributeName] = ColorWithRGB(0xc8c8c8);
    attrs[NSFontAttributeName] = [UIFont fontWithName:@"PingFangSC-Medium" size:15 * SCREENH6];
    getMoneyCountField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"$50" attributes:attrs];
    getMoneyCountField.font = [UIFont fontWithName:@"PingFangSC-Regular" size:15 * SCREENH6];
    getMoneyCountField.textColor = ColorWithRGB(0x646464);
    getMoneyCountField.keyboardType = UIKeyboardTypeDecimalPad;
    getMoneyCountField.keyboardAppearance = UIKeyboardAppearanceAlert;
    
    self.getMoneyCountField = getMoneyCountField;
    
    [self.view addSubview:self.getMoneyCountField];
    
    
    UILabel * getMoneyCountLabel = [[UILabel alloc] init];
    
    getMoneyCountLabel.font = [UIFont fontWithName:@"PingFangSC-Medium" size:16 * SCREENH6];
    getMoneyCountLabel.textColor = ColorWithRGB(0xc8c8c8);
    getMoneyCountLabel.text = @"提现金额";
    
    self.getMoneyCountLabel = getMoneyCountLabel;
    
    [self.view addSubview:self.getMoneyCountLabel];
    
    
    UIView * twoLineView = [UIView new];
    
    twoLineView.backgroundColor = [UIColor whiteColor];
    twoLineView.alpha = 0.1;
    
    self.twoLineView = twoLineView;
    
    [self.view addSubview:self.twoLineView];
    
    
    UIButton * nextButton = [[UIButton alloc] init];
    
    [nextButton setTitle:@"提交提现" forState:UIControlStateNormal];
    nextButton.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Medium" size:17 * SCREENH6];//
    [nextButton setBackgroundImage:[UIImage imageNamed:@"注册"] forState:UIControlStateNormal];
    nextButton.layer.borderColor = [[UIColor blackColor] CGColor];
    //边框宽度
    nextButton.layer.borderWidth =1.0f;
    //处理圆角
    nextButton.layer.cornerRadius =5.0f;
    [nextButton setTitleColor:ColorWithRGB(0xffffff) forState:UIControlStateNormal];
    [nextButton addTarget:self action:@selector(didNextButton) forControlEvents:UIControlEventTouchUpInside];
    
    self.nextButton = nextButton;
    
    [self.view addSubview:self.nextButton];
}


- (void)setupPivkerView{
    
    self.bgView = [[UIView alloc]init];
    self.bgView.frame = CGRectMake(0, SCREENH_HEIGHT, SCREEN_WIDTH, SCREENH_HEIGHT / 3);
    self.bgView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.bgView];
    
    UIView *btnView = [[UIView alloc]init];
    btnView.frame = CGRectMake(0, 0, self.view.frame.size.width, 40 * SCREENH6);
    btnView.backgroundColor = [UIColor lightGrayColor];
    [self.bgView addSubview:btnView];
    
    UIButton *sureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    sureBtn.frame = CGRectMake(10 * SCREENH6, 5 * SCREENH6, 60 * SCREENH6, 30 * SCREENH6);
    [sureBtn setTitle:@"取消" forState:UIControlStateNormal];
    [sureBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [sureBtn addTarget:self action:@selector(cancelBtn) forControlEvents:UIControlEventTouchUpInside];
    [btnView addSubview:sureBtn];
    
    UIButton *cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    cancelBtn.frame = CGRectMake(self.view.frame.size.width - 70 * SCREENH6, 5 * SCREENH6, 60 * SCREENH6, 30 * SCREENH6);
    [cancelBtn setTitle:@"确定" forState:UIControlStateNormal];
    [cancelBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [cancelBtn addTarget:self action:@selector(sureBtn) forControlEvents:UIControlEventTouchUpInside];
    [btnView addSubview:cancelBtn];
    
    float height = SCREENH_HEIGHT / 3 - 40 * SCREENH6;
    self.pickerView = [[UIPickerView alloc]init];
    self.pickerView.frame = CGRectMake(0, 40 * SCREENH6, SCREEN_WIDTH, height);
    self.pickerView.delegate = self;
    self.pickerView.dataSource = self;
    [self.bgView addSubview:self.pickerView];
}


- (void)viewDidLayoutSubviews{
    
    [self.backImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.top.bottom.mas_equalTo(self.view);
    }];
    
    [self.chooseBankCardLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.view).offset(15 * SCREENH6);
        make.top.mas_equalTo(self.view).offset(89 * SCREENH6);
    }];
    
    [self.rightImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(self.view).offset(-15 * SCREENH6);
        make.centerY.mas_equalTo(self.chooseBankCardLabel);
        make.width.mas_equalTo(10 * SCREENH6);
        make.height.mas_equalTo(18 * SCREENH6);
    }];
    
    [self.chooseBankButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(self.rightImageView.mas_leading);
        make.leading.mas_equalTo(self.chooseBankCardLabel.mas_trailing);
        make.centerY.mas_equalTo(self.chooseBankCardLabel);
        make.height.mas_equalTo(40 * SCREENH6);
    }];
    
    [self.bankLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(self.rightImageView.mas_leading);
        make.centerY.mas_equalTo(self.chooseBankCardLabel);
    }];
    
    [self.oneLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.view).offset(129 * SCREENH6);
        make.leading.trailing.mas_equalTo(self.view);
        make.height.mas_equalTo(1 * SCREENH6);
    }];
    
    [self.moneyCountLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.oneLineView.mas_bottom).offset(25 * SCREENH6);
        make.leading.mas_equalTo(self.chooseBankCardLabel);
    }];
    
    [self.moneyNumberField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.moneyCountLabel.mas_trailing).offset(2 * SCREENH6);
        make.centerY.mas_equalTo(self.moneyCountLabel);
    }];
    
    
    [self.getMoneyCountField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.moneyCountLabel.mas_bottom).offset(20 * SCREENH6);
        make.trailing.mas_equalTo(self.view).offset(-15 * SCREENH6);
        make.width.mas_equalTo(273 * SCREENH6);
        make.height.mas_equalTo(55 * SCREENH6);
    }];
    
    [self.getMoneyCountLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.moneyCountLabel);
        make.centerY.mas_equalTo(self.getMoneyCountField);
    }];
    
    [self.twoLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.height.mas_equalTo(self.oneLineView);
        make.top.mas_equalTo(self.getMoneyCountField.mas_bottom).offset(36 * SCREENH6);
    }];
    
    [self.nextButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.twoLineView.mas_bottom).offset(85 * SCREENH6);
        make.leading.mas_equalTo(self.view).offset(15 * SCREENH6);
        make.trailing.mas_equalTo(self.view).offset(-15 * SCREENH6);
        make.height.mas_equalTo(46 * SCREENH6);
    }];
}



// 列数
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

// 每列有多少行
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    
    return self.dataArray.count;
}


// 返回每一行的内容
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    
    NSDictionary * dict = self.dataArray[row];
    
    self.bankNo = dict[@"bankNo"];
    self.bankName = dict[@"bankName"];
    _bankIndex = row;
    
    NSString * bankInfo;
    
    if (self.bankNo.length == 19) {
        
        NSString * bankNum = [self.bankNo substringFromIndex:15];
        
        bankInfo = [NSString stringWithFormat:@"%@(尾号%@)",self.bankName,bankNum];
        
    }else{
        NSString * bankNum = [self.bankNo substringFromIndex:12];
        
        bankInfo = [NSString stringWithFormat:@"%@(尾号%@)",self.bankName,bankNum];
        
    }
    
    return bankInfo;
}


- (void)cancelBtn{
    
    
    [UIView animateWithDuration:0.5 animations:^{
        self.bgView.frame = CGRectMake(0, self.view.frame.size.height, self.view.frame.size.width, SCREENH_HEIGHT / 3);
    } completion:^(BOOL finished) {
    }];
    
}


- (void)sureBtn{
    
    NSDictionary * dict = self.dataArray[_bankIndex];
    
    self.bankNo = dict[@"bankNo"];
    self.bankName = dict[@"bankName"];
    self.bankid = [NSString stringWithFormat:@"%@",dict[@"id"]];//dict[@"id"];
    
    NSString * bankNum = [self.bankNo substringFromIndex:15];
    NSString * bankInfo = [NSString stringWithFormat:@"%@(尾号%@)",self.bankName,bankNum];
    
    self.bankLabel.text = bankInfo;
    
    [UIView animateWithDuration:0.5 animations:^{
        self.bgView.frame = CGRectMake(0, self.view.frame.size.height, self.view.frame.size.width, SCREENH_HEIGHT / 3);
    } completion:^(BOOL finished) {
    }];
}



- (void)didChooseOtherBank{
    
    [UIView animateWithDuration:0.5 animations:^{
        self.bgView.frame = CGRectMake(0, SCREENH_HEIGHT - SCREENH_HEIGHT / 3, SCREEN_WIDTH, SCREENH_HEIGHT / 3);
    }];
    
}


- (void)didNextButton{
    
//    NSLog(@"==%@",self.bankLabel.text);
//    NSLog(@"==%@",self.getMoneyCountField.text);
    
    if ([self.getMoneyCountField.text isEqualToString:@""] || [self.bankLabel.text isEqualToString:@""] || !self.bankLabel.text) {
        
        UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                       message:@"请完善出金信息！"
                                                                preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel
                                                              handler:^(UIAlertAction * action) {
                                                                  //响应事件
//                                                                  NSLog(@"action = %@", action);
                                                              }];
        
        [alert addAction:defaultAction];
        [self presentViewController:alert animated:YES completion:nil];
        
    }else{
       
        [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        
        NSDictionary * parameters = @{@"bankId":self.bankid,@"amount":self.getMoneyCountField.text};
        [[NetworkTool sharedTools] POST:@"user/gold" parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary *  _Nullable responseObject) {
            [MBProgressHUD hideHUDForView:self.view animated:YES];
            NSString * code = [NSString stringWithFormat:@"%@",responseObject[@"code"]];
            
            if ([code isEqualToString:@"0"]) {
                
                UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                               message:@"恭喜你，提交出金审核成功！"
                                                                        preferredStyle:UIAlertControllerStyleAlert];
                
                UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel
                                                                      handler:^(UIAlertAction * action) {
                                                                          //响应事件
                                                                          [self.navigationController popViewControllerAnimated:YES];
                                                                      }];
                
                [alert addAction:defaultAction];
                [self presentViewController:alert animated:YES completion:nil];
                
                [[NSNotificationCenter defaultCenter] postNotificationName:@"loadUserInfo" object:nil];
            }else{
                
                UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                               message:responseObject[@"msg"]
                                                                        preferredStyle:UIAlertControllerStyleAlert];
                
                UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel
                                                                      handler:^(UIAlertAction * action) {
                                                                          //响应事件
                                                                          
                                                                      }];
                
                [alert addAction:defaultAction];
                [self presentViewController:alert animated:YES completion:nil];
                
            }
            
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            [MBProgressHUD hideHUDForView:self.view animated:YES];
            NSLog(@"%@",error);
            UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                           message:@"服务器内部错误，请稍后再试！"
                                                                    preferredStyle:UIAlertControllerStyleAlert];
            
            UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel
                                                                  handler:^(UIAlertAction * action) {
                                                                      //响应事件
                                                                      
                                                                  }];
            
            [alert addAction:defaultAction];
            [self presentViewController:alert animated:YES completion:nil];
        }];
        
    }
}


- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    [self.getMoneyCountField resignFirstResponder];
}


- (void)backToAccountView{
    
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
